import cTitle from 'components/title';
import { Toast } from 'vant';
import DList from "@/components/dlist";
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      integral: window.localStorage.integral || '积分',
      checkList: [],
      datas: [],
      page: 1,
      isLoadMore: true,
      total_page: 0,
      loading: false,
      finisend: false,
      checkAll: false,
      collChecked:false,
      timeOutEvent:'',
      footIdArr:[],
      isLoadMore: true,
    };
  },

  activated() {
    this.page = 1;
    this.finisend = false;
    this.loading = false;
    this.getData();
  },

  mounted() {
  },
  methods:{
    async getData() {
      let json = { "i": this.fun.getKeyByI(), "type": this.fun.getTyep(), page: this.page };
      let response = await $http.get('member.member-history.index', json, "加载中");
      if (response.result !== 1) {
        Toast(response.msg);
        this.page = this.page - 1;
        this.isLoadMore = false;
        return;
      }
      let res = response.data;
      this.isLoadMore = true;
      this.total_page = res.last_page;
      if (!this.total_page) {
        this.page = 0;
      }
      this.datas =  response.data.member_histories;
      let footPrintArr = [];
      footPrintArr[0] = this.datas[0];
      this.datas  = this.handlerFootPrint(this.datas,footPrintArr);
    },
    handlerFootPrint(datas,footPrintArr) {
      if(datas.length === 0) return [];
      let i = 0;
      datas.map((item,index) => {
        this.$set(item, 'goodsList', []);
        this.$set(item, 'checked', false);
        this.$set(item.goods, 'delId', item.id);
        this.$set(item.goods, 'owner_id', item.owner_id);
        this.$set(item.goods, 'vip_level_status', item.vip_level_status);
        this.$set(item.goods, 'option_price', item.option_price);
        this.$set(item, 'dataName', this.getDate(item.date_group)); //处理数据格式显示
        if(footPrintArr[i].date_group === (datas[index] && datas[index].date_group)) {
          footPrintArr[i].goodsList.push(datas[index].goods);
        } else {
          i++;
          footPrintArr.push(datas[index]);
          footPrintArr[i].goodsList.push(datas[index].goods);
        }
        footPrintArr[i].goodsList.map((goodsItem,goodsIndex) => {
          this.$set(goodsItem, 'goodsChecked', false);
          this.$set(goodsItem, 'maskChecked', false);
        })
        if(this.collChecked) { //分页全选的判断
          item.checked = this.collChecked;
          footPrintArr[i].goodsList.map((goodsItem,goodsIndex) => {
            goodsItem.goodsChecked = item.checked;
          });
        }
        if(item.checked) { //只选择日期的判断
          footPrintArr[i].goodsList.map((goodsItem,goodsIndex) => {
            goodsItem.goodsChecked = item.checked;
          });
        }
      });
      return footPrintArr;
    },
    getDate(str) { //处理日期时间
      if(!str) return;
      let strArr = str.split('-');
      let year=strArr[0];
      let month=strArr[1].replace("月",'');
      let day=strArr[2];
      let date = (year + '年' + month + '月' + day + '日').toString();
      return date;
    },
    operateOn() { //切换足迹
      if(this.datas.length === 0) {
        this.$toast("您还没有浏览过任何商品");
        return false;
      }
      this.checkAll = !this.checkAll;
      if(this.checkAll) {
        this.datas.map((item,index) => {
          item.goodsList.map((goodsItem,goodsIndex) => {
            goodsItem.maskChecked = false;
          })
        })
      }
    },
    timeCheck(arr,checked) { //选择时间的删除
      let footIdArr = [];
      arr.forEach((goodsItem,goodsIndex) => {
        goodsItem.goodsChecked = checked;
        if(goodsItem.goodsChecked) {
          footIdArr.push(goodsItem.delId);
        }
      })
      this.collChecked = this.datas.every((item) => item.checked);
      this.footIdArr = footIdArr;
    },
    goodsCheck(arr,goodsItem) { //单选删除
      let footIdArr = [];
      goodsItem.goodsChecked = !goodsItem.goodsChecked;
      arr.checked = arr.goodsList.every((item) => item.goodsChecked);
      this.datas.map((item,index) => {
        item.goodsList.map((goodsItem,goodsIndex) => {
          if(goodsItem.goodsChecked) {
            footIdArr.push(goodsItem.delId);
          }
        })
      })
      this.collChecked = this.datas.every((item) => item.checked);
      this.footIdArr = footIdArr;
    },
    footListDel() {
      this.delteteItem();
    },
    allClick() { //全选
      this.datas.map((item,index) => {
        item.checked =  this.collChecked;
        item.goodsList.map((goodsItem,goodsIndex) => {
          goodsItem.goodsChecked = this.collChecked;
        })
      })
    },
    delteteItem(item, index,goodsIndex) { //删除足迹商品
      let that = this;
      let json = {};
      if(this.collChecked) {
        json.has_all = true;
      } else {
        
      }
      if(this.checkAll && !this.collChecked) {
        json.id = this.footIdArr;
      } else {
        json.id = item?.delId || '';
      }
      $http.post('member.member-history.destroy', json, "处理中...").then(function (response) {
        if (response.result == 1) {
          if(!that.checkAll && !that.collChecked) {
            that.datas[index].goodsList.length === 1?that.datas.splice(index,1):that.datas[index].goodsList.splice(goodsIndex,1);
          } else {
            that.page = 1;
            that.getData();
          }
          that.checkAll = false;
          // that.datas.splice(i, 1);
          Toast("删除成功");
        } else {
          Toast(response.msg);
        }

      }, function (response) {
        console.log(response);
      });

    },
    maskClick(goodsItem,index,goodsIndex) {
      this.delteteItem(goodsItem,index,goodsIndex);
    },
    gtouchstart(item) {
      let self = this;
      this.timeOutEvent = setTimeout(() => {
        if(!this.checkAll) {
        self.longPress(item);
        }
      }, 500); //这里设置定时器，定义长按500毫秒触发长按事件
      return false;
    },
    //手释放，如果在500毫秒内就释放，则取消长按事件，此时可以执行onclick应该执行的事件
    showDeleteButton(item) {
      clearTimeout(this.timeOutEvent); //清除定时器
      if (this.timeOutEvent != 0) {
        //这里写要执行的内容（如onclick事件）
        console.log("点击但未长按");
        if(!this.checkAll && !item.maskChecked) { //未开启选择跳转
          if(item.owner_id != 0){
            this.$router.push(this.fun.getUrl('goodsO2O',{id: item.id,tag:"o2o",store_id:item.owner_id}));
          }else{
            this.$router.push(this.fun.getUrl('goods',{id: item.id}));
          }
        }
        
      }
      return false;
    },
    //如果手指有移动，则取消所有事件，此时说明用户只是要移动而不是长按
    gtouchmove() {
      clearTimeout(this.timeOutEvent); //清除定时器
      this.timeOutEvent = 0;
    },
    //真正长按后应该执行的内容
    longPress(val) {
      this.timeOutEvent = 0;
      this.datas.map((item,index) => {
        item.goodsList.map((goodsItem,goodsIndex) => {
          goodsItem.maskChecked = false;
        })
      })
      val.maskChecked = true;
      //执行长按要执行的内容，如弹出菜单
    },
    //获取更多数据
    getMoreData() { 
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http.get("member.member-history.index", { page: this.page }, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data.member_histories;
              let footPrintArr = [];
              footPrintArr[0] = myData[0];
              this.$nextTick(() => {
                let datas  = this.handlerFootPrint(myData,footPrintArr); //处理数据结构
                datas.map((item,index) => {
                  if(this.datas[this.datas.length -1].date_group === datas[0].date_group) {
                    let dataGroup = this.datas[this.datas.length -1].goodsList;
                    this.datas[this.datas.length -1].goodsList = [...dataGroup,...item.goodsList];
                    datas.shift() 
                  }
                })
                this.datas = [...this.datas,...datas];
              });
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            // error callback
          }
        );
      }
    },
  },
  components: {
    cTitle,
    DList
  }
};